/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.gui.wizard;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;
import javax.swing.text.JTextComponent;

import net.miginfocom.swing.MigLayout;

import org.bugstat.gui.PropertyTracker;
import org.bugstat.gui.UIStrings;
import org.bugstat.renderers.ReportRenderer;
import org.pietschy.wizard.PanelWizardStep;

/**
 * This page asks user for output properties (such as destination folder and output format)
 *  * <pre>
 *     +--------------------------+
 *     | OF_LBL: |OUT_FLDR|       |
 *     |                          |
 *     | FORMATS:                 |     
 *     |   +-----------------+    |
 *     |   | |0| HTML        |    |
 *     |   | |X| CSV         |    |
 *     |   |                 |    |
 *     |   +-----------------+    |
 *     +--------------------------+
 * </pre> 
 *
 * @author Basil Shikin
 */
public class SelectOutputPropertiesPage
    extends PanelWizardStep
{
    private static final long serialVersionUID = 8338359357995879235L;
    
    private final CheckboxList<ReportRenderer> rendererList;
    private final JTextField outputFolder;

    private final PropertyTracker tracker ;
    
    public SelectOutputPropertiesPage()
    {
        setName(UIStrings.TEXT_OUTPUT_PROPERTIES);
        setSummary(UIStrings.TEXT_OUTPUT_SUMMARY);

        tracker = new PropertyTracker();
        
        final JLabel outputFolderLabel = new JLabel( UIStrings.TEXT_OUTPUT_FOLDER + ":" );
        outputFolder = new JTextField();
        trackComponent( outputFolder, "bugstat.gui.mainframe", FileSystemView.getFileSystemView().getDefaultDirectory().getAbsolutePath() );
        
        final JButton browseButton = new JButton( UIStrings.TEXT_CHANGE + "...");
        browseButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                handleBrowse();
            }
        });
        
        final JPanel browsePanel = new JPanel( new MigLayout("fillx", "0[]5[]5[]0", "0[]0") );
        browsePanel.add( outputFolderLabel );
        browsePanel.add( outputFolder, "growx" );
        browsePanel.add( browseButton, "wmin button" );
        
        final JLabel selectRenderersLabel = new JLabel( UIStrings.TEXT_SELECT_OUTPUT_FORMATS + ":");
        rendererList = new CheckboxList<ReportRenderer>();
        final JScrollPane rendererListWrapper = new JScrollPane( rendererList );

        
        setLayout( new MigLayout("fill", "9[grow]9", "9[]9[]5[]push") );
        add( browsePanel, "growx, wrap" );
        add( selectRenderersLabel, "wrap" );
        add( rendererListWrapper, "grow, hmin 50" );
        
        setComplete( true );
    }

    void setReportRenderers(final Collection<ReportRenderer> renderers)
    {
        for ( ReportRenderer renderer : renderers )
        {
            rendererList.addObject( renderer, renderer.getName() );
        }
    }

    Collection<ReportRenderer> getSelectedRenderers()
    {
        return rendererList.getSelectedObjects();
    }
    
    String getSelectedOutputFolder()
    {
        return outputFolder.getText();
    }
    
    private void handleBrowse()
    {
        final JFileChooser fileChooser = new JFileChooser( outputFolder.getText() );
        fileChooser.setApproveButtonText( UIStrings.TEXT_SELECT );
        fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
        
        final int ret = fileChooser.showOpenDialog( this );
        if( ret == JFileChooser.APPROVE_OPTION ) 
        {
            outputFolder.setText( fileChooser.getSelectedFile().getAbsolutePath() );
        }
    }
    
    private void trackComponent(JTextComponent field, String key, String defaultValue)
    {
        final String value = tracker.get( key, defaultValue);
        field.setText( value );
        
        tracker.trackTextComponent( field, key);
    }
}
